package view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.concerneyes.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Depiction:弹出菜单
 * <p>
 * Modify:
 * <p>
 * Author: Kevin Lynn
 * <p>
 * Create Date：2016年2月10日 上午12:12:55
 * <p>
 *
 * @version 1.0
 * @since 1.0
 */
public class PopMenu implements OnItemClickListener {
    private final Activity mContext;
    private int menuId;
    private PopupWindow popup;
    private ListView listview;
    private List<String> itemList;
    private List<Integer> iconList;
    private OnMenuItemClickListener onMenuItemClickListener;

    public PopMenu(Activity activity) {
        this.mContext = activity;
        this.itemList = new ArrayList<String>();
        this.iconList = new ArrayList<Integer>();

        LayoutInflater inflater = LayoutInflater.from(mContext);
        listview = (ListView) inflater.inflate(R.layout.listview_dialog, null);
        listview.setFocusable(true);
        listview.setFocusableInTouchMode(true);
        listview.setOnItemClickListener(this);

        popup = new PopupWindow(mContext);
        popup.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.title_function_bg));
        popup.setWidth(mContext.getWindowManager().getDefaultDisplay().getWidth() / 4);
        popup.setHeight(LayoutParams.WRAP_CONTENT);
        popup.setOutsideTouchable(true);
        popup.setFocusable(true);
        popup.setContentView(listview);


        popup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
                lp.alpha = 1f;
                mContext.getWindow().setAttributes(lp);
            }
        });
    }

    public void showShadow() {
        WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
        lp.alpha = 0.4f;
        mContext.getWindow().setAttributes(lp);
    }

    public int getId() {
        return menuId;
    }

    public void setId(int id) {
        this.menuId = id;
    }

    public OnMenuItemClickListener getOnMenuItemClickListener() {
        return onMenuItemClickListener;
    }

    public void setOnMenuItemClickListener(OnMenuItemClickListener onMenuItemClickListener) {
        this.onMenuItemClickListener = onMenuItemClickListener;
    }

    public void addItem(String item, int icon) {
        if (!TextUtils.isEmpty(item)) {
            itemList.add(item);
        }

        iconList.add(icon);
    }

    public void addItems(String[] items, int[] icons) {
        if (items != null && items.length > 0) {
            for (String item : items) {
                itemList.add(item);
            }
        }

        if (icons != null && icons.length > 0) {
            for (int icon : icons) {
                iconList.add(icon);
            }
        }
    }

    public void showAsDropDown(View anchor) {
        showAsDropDown(anchor, 0, 0);
    }

    public void showAsDropDown(View anchor, int xoff, int yoff) {
        listview.setAdapter(new MenuAdapter());
        popup.showAsDropDown(anchor, xoff, yoff);
        popup.update();
    }

    public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) {
        listview.setAdapter(new MenuAdapter());
        popup.showAtLocation(anchor, gravity, xoff, yoff);
        popup.update();
    }

    public void dismiss() {
        popup.dismiss();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        popup.dismiss();

        if (onMenuItemClickListener != null) {
            onMenuItemClickListener.onMenuItemClick(menuId, position);
        }
    }

    class MenuAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return itemList.size();
        }

        @Override
        public String getItem(int position) {
            return itemList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_popwindown, null);
                holder.mPopIcon = (ImageView) convertView.findViewById(R.id.pop_icon);
                holder.mPopText = (TextView) convertView.findViewById(R.id.pop_text);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            Log.e(this.getClass().getSimpleName(),"getItem(position)==>"+getItem(position));
            holder.mPopText.setText(getItem(position));
            try {
                holder.mPopIcon.setImageDrawable(mContext.getResources().getDrawable(iconList.get(position)));
            } catch (Exception e) {
                holder.mPopIcon.setVisibility(View.GONE);
            }
            return convertView;
        }

    }

    class ViewHolder {
        ImageView mPopIcon;
        TextView mPopText;
    }

    public int dip2px(float dpValue) {
        final float scale = mContext.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public interface OnMenuItemClickListener {
        void onMenuItemClick(int menuId, int position);
    }
}
